#include "zadanie7lib.h"
#include "eulerApp.h"
#include "eulerpcApp.h"
#include "rk2App.h"
#include <iostream>
#include <cstdlib>
#include <math.h>


using namespace std;
namespace rkwolek {

    double exact(double t) {
        return exp(-100 * t);
        //return (12 * exp(t) / (pow(1 + exp(t), 2)));
    }

    Application::Application(int methodNum, double(*f)(double, double)) {
        this->factory = getFactory(methodNum, f);
    }

    MetodaApp * Application::getFactory(int methodNum, double(*f)(double, double)) {
        switch (methodNum) {
            case 0: return new EulerApp(0, 1, 2, f);
            case 1: return new EulerpcApp(0, 1, 2, f);
            case 2: return new Rk2App(0, 1, 2, f);
            default: return new EulerApp(0, 1, 2, f);
        }
    }

    void Application::normalize() {
        // z maina z wykladu
        double *soln = factory->function(100); // calling euler method
        double norm = 0;
        double h = 2.0 / 100;

        for (int k = 0; k <= 100; k++) {// compare error
            norm = max(norm, fabs(exact(k * h) - soln[k]));
            //cout << "max norm of euler method error is " << norm << '\n';
            cout << "soln[" << k << "] " << soln[k] << '\n';
        }
    }
}
